package com.wy2.mapper;


import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.query.Param;

import com.wy2.entity.Permission;
import com.wy2.util.FenyeUtil;

public interface PermissionMapper{
	
	//删除
	@Delete("delete from permission where id =#{ids}")
	Integer delete(@Param("ids")String ids);

	//修改
	@Update("update permission set pvalue = #{#per.pvalue},pmodules = #{#per.pmodules},pname = #{#per.pname},"
			+ "plastupdatetime = #{#per.plastupdatetime} where pid = #{#per.pid}")
	Integer update(@Param("per")Permission per);
	
	// 获取当前角色的应该有的权限增删改
	@Select("select p.id from permission p inner join rolepermission rp "
			+ "on p.id = rp.pid where rp.rid = #{rid} and p.parentid != '0'")
	List<Integer> getById(String rid);
	
	// 先根据角色id删除所有数据
	@Delete("delete from rolepermission where rid = #{rid}")
	Integer deleteRidByid(String rid);

	// 添加权限
	@Insert("insert into rolepermission(rid,pid) values(#{rid},#{setid})")
	Integer addRidPid(@Param("rid") String rid, @Param("setid") String setid);
	
	/**
	 * 用户的增删改权限
	 * @param id
	 * @return
	 */
	@Select("select p.* from permission p " + 
			"inner join rolepermission rp on p.id = rp.pid " +
			"inner join userrole ur on rp.rid = ur.rid " +
			"inner join user u on u.uid = ur.uid " +
			"where u.uid = #{id}")
	List<Permission> getAll(@Param("id")String id);
	
	/**
	 * 获取用户是否拥有该权限
	 * @param id
	 * @return
	 */
	@Select("select p.* from permission p " + 
			"inner join rolepermission rp on p.id = rp.pid " +
			"inner join userrole ur on rp.rid = ur.rid " +
			"inner join user u on u.uid = ur.uid " +
			"where u.uid = #{id} and p.pvalue = #{pvalue}")
	List<Permission> getAllByPvalue(@Param("id")String id,@Param("pvalue")String pvalue);
	
	//获取权限模块点击显示区域的
	@Select("select * from permission where id = #{id}")
	Permission getPer(@Param("id")String id);
	@Select("select * from permission")
	List<Permission> findAll();
	Permission add(Permission per);
	
	
	Integer getCount(FenyeUtil<Permission> fenye);

	List<Permission> getAlls(FenyeUtil<Permission> fenye);
}




















